﻿using System;
using System.Collections;
using System.Windows.Forms;
using AppCommon.Framework;
using AppResources.Res;
using TMSIM.Client.CM.Business;
using SysCommon;
using SysCommon.Entity.Human;
using SysCommon.Interface.ManageUserSession;
using SysCommon.UI;

namespace TMSIM.Client.CM.UI.MainFrm
{
    public partial class MenuForm : Form
    {
        public MenuForm()
        {
            InitializeComponent();

            try
            {
                btn_MainClose.Image_A = Resources.GetImage("main_close_a.jpg");
                btn_MainClose.Image_B = Resources.GetImage("main_close_b.jpg");
                btn_MainClose.Image_C = Resources.GetImage("main_close_c.jpg");
                btn_MainClose.buttonBMPClick += new ButtonBMPClick(mu_item_quit_Click);
                btn_MainMin.Image_A = Resources.GetImage("main_min_a.jpg");
                btn_MainMin.Image_B = Resources.GetImage("main_min_b.jpg");
                btn_MainMin.Image_C = Resources.GetImage("main_min_c.jpg");
                btn_MainMin.buttonBMPClick += new ButtonBMPClick(btn_MainMin_buttonBMPClick);
                InitMenuFunction();

                MenuAllVisible(false);
                ShowPopedom();
                //MenuAllVisible(true);

                InitTopMenu(mu_top_map);
                InitTopMenu(mu_top_watch);
                InitTopMenu(mu_top_transgress);
                InitTopMenu(mu_top_callpolice);
                InitTopMenu(mu_top_basedatas);
                InitTopMenu(mu_top_dictate);
                InitTopMenu(mu_top_help);
            }
            catch (Exception ex)
            {
                Global.mainLog.Error("MenuForm()出错", ex);
            }
        }

        private void InitTopMenu(ToolStripMenuItem TopMenu)
        {
            MenuHelper m_MenuHelper1 = new MenuHelper();
            m_MenuHelper1.TopMenu = TopMenu;
            m_MenuHelper1.InitMenu();
        }

        private void mu_item_quit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btn_MainMin_buttonBMPClick(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void SetmenuVisible(string menuKey)
        {
            try
            {
                string menu_name = munus[menuKey].ToString();

                for (int i = 0; i < mu_Main.Items.Count; i++)
                {
                    ToolStripItem item0 = mu_Main.Items[i];
                    //string m_name = item0.Name;
                    ToolStripMenuItem item = (ToolStripMenuItem) item0;
                    for (int j = 0; j < item.DropDownItems.Count; j++)
                    {
                        ToolStripItem item1 = item.DropDownItems[j];
                        string m_name = item1.Name;
                        if (!m_name.StartsWith(menu_name)) continue;
                        item1.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Global.mainLog.Error("MenuForm()中的 SetmenuVisible(string menuKey)方法出错", ex);
            }
        }

        private void MenuAllVisible(bool isVisible)
        {
            try
            {
                for (int i = 0; i < mu_Main.Items.Count; i++)
                {
                    ToolStripItem item0 = mu_Main.Items[i];
                    ToolStripMenuItem item = (ToolStripMenuItem) item0;
                    for (int j = 0; j < item.DropDownItems.Count; j++)
                    {
                        ToolStripItem item1 = item.DropDownItems[j];
                        string m_name = item1.Name;
                        if (m_name.StartsWith("menu_")) item1.Visible = isVisible;
                    }
                }
            }
            catch (Exception ex)
            {
                Global.mainLog.Error("MenuForm()中的 MenuAllVisible(bool isVisible)方法出错", ex);
            }
        }

        #region 取权限信息

        private void ShowPopedom()
        {
            string mPopedom = "";

            #region 根据当前操作员所属机构定位显示

            try
            {
                Global.mainLog.Debug("MenuForm()中开始调用 IMUS_ManageUserSessionBizCtrl.GetCurrentUser()");
                IMUS_ManageUserSessionBizCtrl m_modul =
                    (IMUS_ManageUserSessionBizCtrl) SystemFactory.GetBizController(
                                                        typeof (IMUS_ManageUserSessionBizCtrl));
                User_Ety oneEty = m_modul.GetCurrentUser();
                mPopedom = oneEty.RIGHTSERIAL;

                Global.mainLog.Debug("MenuForm()中调用 IMUS_ManageUserSessionBizCtrl.GetCurrentUser() 完成");
            }
            catch (Exception ex)
            {
                Global.mainLog.Error("MenuForm()中调用 IMUS_ManageUserSessionBizCtrl.GetCurrentUser() 方法出错", ex);
            }

            #endregion

            Global.mainLog.Debug("MenuForm() 完成1");
            if (mPopedom == null) return;
            if (mPopedom.Length == 0) return;
            Global.mainLog.Debug("MenuForm() 完成2");
            try
            {
                string[] popedoms = mPopedom.Split(',');
                Global.mainLog.Debug("MenuForm() 完成3");
                foreach (string oneStr in popedoms)
                {
                    if (oneStr.Trim().Length == 0) continue;
                    SetmenuVisible(oneStr);
                }
                Global.mainLog.Debug("MenuForm() 完成4");
            }
            catch (Exception ex)
            {
                Global.mainLog.Error("MenuForm()中的ShowPopedom()方法出错", ex);
            }
        }

        #endregion

        #region InitMenuFunction()

        private Hashtable munus = new Hashtable();

        private void InitMenuFunction()
        {
            //地图
            //munus.Add("a1", "menu_OpenMap");
            //munus.Add("a2", "menu_Maps");
            munus.Add("a3", "menu_MapOperation");
            munus.Add("a4", "menu_QueryMap");
            munus.Add("a5", "menu_rules");
            munus.Add("a6", "menu_MapLayer");
            munus.Add("a7", "menu_MapLegend");
            munus.Add("a8", "menu_item_quit");//6

            //监控
            munus.Add("b1", "menu_SelectObject");
            munus.Add("b2", "menu_VideoStakeOut");
            munus.Add("b3", "menu_HistoryLoop");
            munus.Add("b4", "menu_OnLineQuery");
            munus.Add("b5", "menu_StatOnLineTime");
            munus.Add("b6", "menu_StatOnlineForArea");
            munus.Add("b7", "menu_StatOnlineSpacialDistributing");
            munus.Add("b8", "menu_StatDistance");//8
            munus.Add("b9", "menu_SpeedStakeOut");
            munus.Add("b10","menu_PeccancyStakeOut");

            //违章
            munus.Add("c1", "menu_TransgressQuery");
            munus.Add("c2", "menu_StatCarMemberPeccancy");
            //munus.Add("c3", "menu_StatCarMember1PeccancyRate");
            munus.Add("c4", "menu_StatCarMemberBadPeccancyRate");
            munus.Add("c5", "menu_StatAreaPeccancy");
            munus.Add("c6", "menu_StatPeccancySpacialDistributing");
            munus.Add("c7", "menu_StatRoadPeccancy");

            //报警
            munus.Add("d1", "menu_CallPoliceQuery");
            munus.Add("d2", "menu_StatCallPoliceForCarMember");
            munus.Add("d3", "menu_StatCallPoliceForArea");
            munus.Add("d4", "menu_StatCallPoliceSpacialDistributing");
            munus.Add("d5", "menu_StatCallPoliceByRoad");

            //短信
            munus.Add("e1", "menu_SendMessage");
            munus.Add("e2", "menu_QueryHistoryForSMS");

            //数据
            munus.Add("f1", "menu_OrgMgmt");
            munus.Add("f2", "menu_CarmemberMgmt");
            munus.Add("f3", "menu_VehicleMgmt");
            munus.Add("f4", "menu_CarmemberVehicleStat");
            munus.Add("f5", "menu_AreaVehicleStat");
            munus.Add("f6", "menu_DriverMgmt");
            munus.Add("f7", "menu_limitSpeed");
            munus.Add("f8", "menu_DangerPlan");
            munus.Add("f9", "menu_UserMgmt");
            munus.Add("f10", "menu_ModifyUserPassword");
            munus.Add("f11", "menu_PopedomMgmt");
            munus.Add("f12", "menu_SysConfig");
            munus.Add("f13", "menu_DangerCargoManagement");
            munus.Add("f14", "menu_DangerEnterpriseManagement");
            munus.Add("f15", "menu_EscortManagement");

            //帮助
            munus.Add("g1", "menu_HelpDir");
            munus.Add("g2", "menu_HelpIndex");
            munus.Add("g3", "menu_Logs");
            munus.Add("g4", "menu_updateOnline");
            munus.Add("g5", "menu_item_AboutSystem");
        }

        #endregion
    }
}